<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: admincp_feed.php 7071 2010-03-28 11:17:10Z chenchunshao $
 */

if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}

$detail = $_G['gp_detail'];
$uid = $_G['gp_uid'];
$users = $_G['gp_users'];
$feedid = $_G['gp_feedid'];
$icon = $_G['gp_icon'];
$hot1 = $_G['gp_hot1'];
$hot2 = $_G['gp_hot2'];
$starttime = $_G['gp_starttime'];
$endtime = $_G['gp_endtime'];
$searchsubmit = $_G['gp_searchsubmit'];
$feedids = $_G['gp_feedids'];
$ppp = $_G['ppp'];

cpheader();

if(!submitcheck('feedsubmit')) {

	$starttime = !preg_match("/^(0|\d{4}\-\d{1,2}\-\d{1,2})$/", $starttime) ? dgmdate(TIMESTAMP - 86400 * 7, 'Y-n-j') : $starttime;
	$endtime = $_G['adminid'] == 3 || !preg_match("/^(0|\d{4}\-\d{1,2}\-\d{1,2})$/", $endtime) ? dgmdate(TIMESTAMP, 'Y-n-j') : $endtime;

	shownav('topic', 'nav_feed');
	showsubmenusteps('nav_feed', array(
		array('feed_search', !$searchsubmit),
		array('nav_feed', $searchsubmit)
	));
	showtips('feed_tips');
	echo <<<EOT
<script type="text/javascript" src="static/js/forum_calendar.js"></script>
<script type="text/JavaScript">
function page(number) {
	$('feedforum').page.value=number;
	$('feedforum').searchsubmit.click();
}
</script>
EOT;
	showtagheader('div', 'searchposts', !$searchsubmit);
	showformheader("feed", '', 'feedforum');
	showhiddenfields(array('page' => $page));
	showtableheader();
	showsetting('feed_search_detail', 'detail', $detail, 'radio');
	showsetting('feed_search_uid', 'uid', $uid, 'text');
	showsetting('feed_search_user', 'users', $users, 'text');
	showsetting('feed_search_feedid', 'feedid', $feedid, 'text');
	showsetting('feed_search_icon', 'icon', $icon, 'text');
	showsetting('feed_search_hot', array('hot1', 'hot2'), array('', ''), 'range');
	showsetting('feed_search_time', array('starttime', 'endtime'), array($starttime, $endtime), 'daterange');
	showsubmit('searchsubmit');
	showtablefooter();
	showformfooter();
	showtagfooter('div');

} else {
	$feedids = authcode($feedids, 'DECODE');
	$feedidsadd = $feedids ? explode(',', $feedids) : $_G['gp_delete'];
	include_once libfile('function/delete');
	$deletecount = count(deletefeeds($feedidsadd));
	$cpmsg = cplang('feed_succeed', array('deletecount' => $deletecount));

?>
<script type="text/JavaScript">alert('<?=$cpmsg?>');parent.$('feedforum').searchsubmit.click();</script>
<?php

}

if(submitcheck('searchsubmit')) {

	$feedids = $feedcount = '0';
	$sql = $error = '';
	$users = trim($users);

	if($users != '') {
		$uids = '-1';
		$query = DB::query("SELECT uid FROM ".DB::table('home_feed')." WHERE username IN ('".str_replace(',', '\',\'', str_replace(' ', '', $users))."')");
		while($feedarr = DB::fetch($query)) {
			$uids .= ",$feedarr[uid]";
		}
		$sql .= " AND f.uid IN ($uids)";
	}

	if($icon != '') {
		$query = DB::query("SELECT icon FROM ".DB::table('home_feed')." WHERE icon ='$icon'");
		$feedarr = DB::fetch($query);
		$icon = $feedarr[icon];
		$sql .= " AND f.icon='$icon'";
	}

	if($starttime != '0') {
		$starttime = strtotime($starttime);
		$sql .= " AND f.dateline>'$starttime'";
	}

	if($_G['adminid'] == 1 && $endtime != dgmdate(TIMESTAMP, 'Y-n-j')) {
		if($endtime != '0') {
			$endtime = strtotime($endtime);
			$sql .= " AND f.dateline<'$endtime'";
		}
	} else {
		$endtime = TIMESTAMP;
	}

	if($feedid != '') {
		$feedids = '-1';
		$query = DB::query("SELECT feedid FROM ".DB::table('home_feed')." WHERE feedid IN ('".str_replace(',', '\',\'', str_replace(' ', '', $feedid))."')");
		while($fidarr = DB::fetch($query)) {
			$feedids .= ",$fidarr[feedid]";
		}
		$sql .= " AND  f.feedid IN ($feedids)";
	}

	if($uid != '') {
		$uids = '-1';
		$query = DB::query("SELECT uid FROM ".DB::table('home_feed')." WHERE uid IN ('".str_replace(',', '\',\'', str_replace(' ', '', $uid))."')");
		while($uidarr = DB::fetch($query)) {
			$uids .= ",$uidarr[uid]";
		}
		$sql .= " AND f.uid IN ($uids)";
	}

	$sql .= $hot1 ? " AND f.hot >= '$hot1'" : '';
	$sql .= $hot2 ? " AND f.hot <= '$hot2'" : '';

	if(($_G['adminid'] == 2 && $endtime - $starttime > 86400 * 16) || ($_G['adminid'] == 3 && $endtime - $starttime > 86400 * 8)) {
		$error = 'feed_mod_range_illegal';
	}

	if(!$error) {
		if($detail) {
			$pagetmp = $page;
			do{
				$query = DB::query("SELECT * FROM ".DB::table('home_feed')." f WHERE 1 $sql ORDER BY f.dateline DESC LIMIT ".(($pagetmp - 1) * $ppp).",{$ppp}");
				$pagetmp--;
			} while(!DB::num_rows($query) && $pagetmp);
			$feeds = '';

			while($feed = DB::fetch($query)) {
				$feed['dateline'] = dgmdate($feed['dateline']);
				include_once libfile('function/feed');
				$feed = mkfeed($feed);

				$feeds .= showtablerow('', '', array(
					"<input class=\"checkbox\" type=\"checkbox\" name=\"delete[]\" value=\"$feed[feedid]\" />",
					$feed['title_template'],
					$feed['body_template'],
					$feed['icon'],
					$feed['dateline']
				), TRUE);
			}
			$feedcount = DB::result_first("SELECT count(*) FROM ".DB::table('home_feed')." f WHERE 1 $sql");
			$multi = multi($feedcount, $ppp, $page, ADMINSCRIPT."?action=feed");
			$multi = preg_replace("/href=\"".ADMINSCRIPT."\?action=feed&amp;page=(\d+)\"/", "href=\"javascript:page(\\1)\"", $multi);
			$multi = str_replace("window.location='".ADMINSCRIPT."?action=feed&amp;page='+this.value", "page(this.value)", $multi);
		} else {
			$feedcount = 0;
			$query = DB::query("SELECT f.feedid FROM ".DB::table('home_feed')." f WHERE 1 $sql");
			while($feed = DB::fetch($query)) {
				$feedids .= ','.$feed['feedid'];
				$feedcount++;
			}
			$multi = '';
		}

		if(!$feedcount) {
			$error = 'feed_post_nonexistence';
		}
	}

	showtagheader('div', 'postlist', $searchsubmit);
	showformheader('feed&frame=no', 'target="feedframe"');
	showhiddenfields(array('feedids' => authcode($feedids, 'ENCODE')));
	showtableheader(cplang('feed_result').' '.$feedcount.' <a href="###" onclick="$(\'searchposts\').style.display=\'\';$(\'postlist\').style.display=\'none\';" class="act lightlink normal">'.cplang('research').'</a>', 'fixpadding');

	if($error) {
		echo "<tr><td class=\"lineheight\" colspan=\"15\">$lang[$error]</td></tr>";
	} else {
		if($detail) {
			showsubtitle(array('', 'feed_title', 'feed_body', 'feed_icon', 'time'));
			echo $feeds;
		}
	}

	showsubmit('feedsubmit', 'delete', $detail ? 'del' : '', '', $multi);
	showtablefooter();
	showformfooter();
	echo '<iframe name="feedframe" style="display:none"></iframe>';
	showtagfooter('div');

}

?>